import { createRouter, createWebHashHistory } from 'vue-router'
import { client, manage, empMenu, table, prompt, llmTrain, approve, msgNotice } from './client.ts'

import { userStoreWithOut } from '@/stores/modules/userStore'
import { ElLoading } from 'element-plus'
import { toSSO, getURLParameter } from '@/common/util'

/**
 * 1. 因为我们分了客户端和管理端两部分，只有管理端需要有Layout导航 所以如下router管理分开了。
 * 2. 引用组建时，这里后缀要带上vue 因为ts知道默认找.vue文件
 *
 */

const children = [client, manage, empMenu, table, prompt, llmTrain, approve, msgNotice]

const routes = [
  {
    path: '/',
    component: () => import('@/layout/AppLayout.vue'),
    redirect: '/client',
    children
  }
]

const router = createRouter({
  // 路由模式
  history: createWebHashHistory(),
  routes,
  strict: false,
  // 切换页面，滚动到最顶部
  scrollBehavior: () => ({ left: 0, top: 0 })
})
const store = userStoreWithOut()
// 检测当前用户是否登录
localStorage.setItem('uname', 'test004')
localStorage.setItem('lantId', '888')
localStorage.setItem('orgId', '888')
localStorage.setItem('orgName', '全省')
localStorage.setItem('staffName', '省级测试用户')
localStorage.setItem(
  'menus',
  '[{"id":"4","parent":null,"text":"智能取数","menuType":null,"description":"","url":"/client","level":null,"state":null,"position":0,"icon":"House"},{"id":"1","parent":null,"text":"日志管理","menuType":null,"description":"iconfont icon-rizhifenxi","url":"/log","level":null,"state":null,"position":0,"icon":"Clock"},{"id":"2","parent":null,"text":"权限管理","menuType":null,"description":"iconfont icon-guanli","url":"/empMenu","level":null,"state":null,"position":0,"icon":"Setting"},{"id":"3","parent":null,"text":"指标管理","menuType":null,"description":"iconfont icon-zhibiaoguanli","url":"/table","level":null,"state":null,"position":0,"icon":"Operation"},{"id":"5","parent":null,"text":"提示词管理","menuType":null,"description":"iconfont icon-tishici","url":"/prompt","level":null,"state":null,"position":0,"icon":"Operation"},{"id":"6","parent":null,"text":"模型训练","menuType":null,"description":"iconfont icon-liliangxunlian","url":"/llmTrain","level":null,"state":null,"position":0,"icon":"Operation"},{"id":"7","parent":null,"text":"我的消息","menuType":null,"description":"iconfont icon-tishici","url":"/msgNotice","level":null,"state":null,"position":0,"icon":"Operation"}]'
)
// localStorage.setItem("token",this.jwtToken)
// localStorage.setItem("accessToken",this.accessToken)
// localStorage.setItem("powerTableList", JSON.stringify(dataArea))
store.setUserInfo(
  '{"userInfo":{"permissions":[],"userId":"test004","loginAcct":"test004","roles":null,"menus":[{"id":"4","parent":null,"text":"智能取数","menuType":null,"description":"","url":"/","level":null,"state":null,"position":0},{"id":"1","parent":null,"text":"日志管理","menuType":null,"description":"iconfont icon-rizhifenxi","url":"/log","level":null,"state":null,"position":0},{"id":"2","parent":null,"text":"权限管理","menuType":null,"description":"iconfont icon-guanli","url":"/empMenu","level":null,"state":null,"position":0},{"id":"3","parent":null,"text":"指标管理","menuType":null,"description":"iconfont icon-zhibiaoguanli","url":"/table","level":null,"state":null,"position":0},{"id":"5","parent":null,"text":"提示词管理","menuType":null,"description":"iconfont icon-tishici","url":"/prompt","level":null,"state":null,"position":0},{"id":"6","parent":null,"text":"模型训练","menuType":null,"description":"iconfont icon-liliangxunlian","url":"/llmTrain","level":null,"state":null,"position":0},{"id":"7","parent":null,"text":"我的消息","menuType":null,"description":"iconfont icon-tishici","url":"/msgNotice","level":null,"state":null,"position":0}],"elements":null,"user":{"userId":"test004","loginAcct":"test004","userName":null,"orgName":"全省","dataArea":null,"staffAccount":"test004","userPassord":null,"staffName":"省级测试用户","mobilePhone":null,"certNumber":null,"innerEmail":null,"workProp":null,"qqNumber":null,"rtx":null,"orgId":"888","loginType":null,"uuid":null,"staffId":null,"orgUuid":null,"staffFixId":null,"accessToken":null,"latnId":"888"},"username":null}}'
)

router.beforeEach((to, from, next) => {
  // next()
  //如果是跳转到问答页面，所有用户放行
  if (to.path === '/') {
    next()
    return
  }
  const menus = JSON.parse(localStorage.getItem('menus'))
  if (
    menus.some((item) => {
      return item.url == to.path
    })
  ) {
    next()
    return
  }
  let errorCode = 200
  // let pathExists = false
  // // 判断是否合法路由
  // for (let path in routePaths) {
  //   if (routePaths[path] === to.path) {
  //     pathExists = true
  //     break
  //   }
  // }
  // if (pathExists) {
  //   // 判断是否授权路由
  //   // errorCode = store.getters['auth/hasRouterPermission'](to.path) ? 200 : 403
  //   errorCode = 200
  // } else {
  //   errorCode = 404
  // }
})
export default router
